package engine.utils
{
	import engine.utils.DisplayUtils;
	
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class TextUtils
	{	
		public static function swap(oldText:TextField, text:String = "", font:String = ""):TextField
		{
			var newText:TextField = TextUtils.copy(oldText, text, font);
			
			DisplayUtils.swapChild(newText, oldText);
			
			return newText;
		}
		
		public static function copy(oldText:TextField, text:String = "", font:String = ""):TextField
		{
			var newText:TextField = new TextField();
			
			newText.name		= oldText.name;
			newText.text 		= (text == "") ? oldText.text : text;
			
			newText.x 			= oldText.x;
			newText.y 			= oldText.y;
			newText.rotation 	= oldText.rotation;
			newText.width 		= oldText.width;
			newText.height 		= oldText.height;
			newText.alpha 		= oldText.alpha;
			newText.visible		= oldText.visible;
			
			newText.alwaysShowSelection 	= oldText.alwaysShowSelection;
			newText.antiAliasType 			= oldText.antiAliasType;
			newText.autoSize 				= oldText.autoSize;
			newText.background 				= oldText.background;
			newText.backgroundColor 		= oldText.backgroundColor;
			newText.border 					= oldText.border;
			newText.borderColor 			= oldText.borderColor;
			newText.condenseWhite 			= oldText.condenseWhite;
			newText.displayAsPassword 		= oldText.displayAsPassword;
			newText.embedFonts 				= true;
			newText.filters					= oldText.filters;
			newText.gridFitType 			= oldText.gridFitType;
			newText.htmlText 				= oldText.htmlText;
			newText.mask					= oldText.mask;
			newText.maxChars 				= oldText.maxChars;
			newText.mouseEnabled 			= false;
			newText.mouseWheelEnabled		= oldText.mouseWheelEnabled;
			newText.multiline 				= oldText.multiline;
			newText.restrict 				= oldText.restrict;
			newText.scrollH 				= oldText.scrollH;
			newText.scrollRect 				= oldText.scrollRect;
			newText.scrollV 				= oldText.scrollV;
			newText.selectable 				= oldText.selectable;
			newText.sharpness 				= oldText.sharpness;
			newText.styleSheet 				= oldText.styleSheet;
			newText.textColor 				= oldText.textColor;
			newText.thickness 				= oldText.thickness;
			newText.transform				= oldText.transform;
			newText.type 					= oldText.type;
			newText.useRichTextClipboard 	= oldText.useRichTextClipboard;
			newText.wordWrap 				= oldText.wordWrap;
			
			var oldFormat:TextFormat = oldText.getTextFormat();
			var newFormat:TextFormat = TextUtils.copyFormat(oldFormat, font);
			
			newText.setTextFormat(newFormat);
			newText.defaultTextFormat = newFormat;
			
			return newText;
		}
		
		public static function copyFormat(oldFormat:TextFormat, font:String = ""):TextFormat
		{
			var newFormat:TextFormat = new TextFormat();
			
			newFormat.align 		= oldFormat.align;
			newFormat.blockIndent 	= oldFormat.blockIndent;
			newFormat.bold 			= oldFormat.bold;
			newFormat.bullet 		= oldFormat.bullet;
			newFormat.color 		= oldFormat.color;
			newFormat.font 			= (font == "") ? oldFormat.font : font;
			newFormat.indent 		= oldFormat.indent;
			newFormat.italic 		= oldFormat.italic;
			newFormat.kerning 		= oldFormat.kerning;
			newFormat.leading 		= oldFormat.leading;
			newFormat.leftMargin 	= oldFormat.leftMargin;
			newFormat.letterSpacing = oldFormat.letterSpacing;
			newFormat.rightMargin 	= oldFormat.rightMargin;
			newFormat.size 			= oldFormat.size;
			newFormat.tabStops 		= oldFormat.tabStops;
			newFormat.target 		= oldFormat.target;
			newFormat.underline 	= oldFormat.underline;
			newFormat.url 			= oldFormat.url;
			
			return newFormat;
		}
	}
}